package com.huaguoguo.cyg.spider.parse;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.huaguoguo.cyg.entity.tl.to.Gang;
import com.huaguoguo.cyg.entity.tl.to.Player;
import com.huaguoguo.cyg.entity.tl.to.ServerInfo;
import com.huaguoguo.cyg.entity.tl.to.Trumpet;
import com.huaguoguo.cyg.exceptions.SpiderParseException;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import us.codecraft.xsoup.Xsoup;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 类说明： 天龙排行榜解析器<br/>
 * author:huaguoguo
 * Date: 2020/2/22
 * Time: 12:13
 */
@Slf4j
public class TLRankParser {

    private static String timeRegex = "[0-9]{4}-[0-1][0-9]-[0-3][0-9]";

    /**
     * 解析帮派排行榜
     *
     * @param document 抓取到的整个网页
     * @param data
     * @return
     */
    public static List<Gang> parseGangRank(Document document, Map<String, Object> data) {
        // 1. 获取所有tr标签的内容
        Elements elements = Xsoup.compile("/html/body/div[1]/table/tbody/tr").evaluate(document).getElements();
        // 解析更新时间
        LocalDate updateDate = parseDate(document);
        // 解析排行榜
        return elements.stream().map(element -> {
            List<String> list = Xsoup.compile("//td/text()").evaluate(element).list();
            if (CollUtil.isEmpty(list)) {
                return null;
            }
            Gang gang = Gang.builder().gangName(list.get(1))
                    .gangAttackNum(Integer.valueOf(list.get(2)))
                    .gangLeader(list.get(3))
                    .peopleNum(Integer.valueOf(StrUtil.trim(list.get(4))))
                    .updateDate(updateDate)
                    .areaId(MapUtil.getInt(data,"areaId"))
                    .build();
            return gang;
        }).filter(Objects::nonNull).collect(Collectors.toList());
    }

    /**
     * 解析人物排行榜
     * @param document
     * @param data
     * @return
     */
    public static List<Player> parsePlayerRank(Document document, Map<String, Object> data) {
        // 1. 获取所有tr标签的内容
        Elements elements = Xsoup.compile("/html/body/div[1]/table/tbody/tr").evaluate(document).getElements();
        // 解析更新时间
        LocalDate updateDate = parseDate(document);
        // 解析排行榜
        return elements.stream().map(element -> {
            List<String> list = Xsoup.compile("//td/text()").evaluate(element).list();
            if (CollUtil.isEmpty(list)) {
                return null;
            }
            Player player = Player.builder().playName(list.get(1))
                    .playScore(Integer.valueOf(list.get(2)))
                    .playLevel(Integer.valueOf(StrUtil.trim(list.get(3))))
                    .profession(StrUtil.trim(list.get(4)))
                    .gangName(StrUtil.trim(list.get(5)))
                    .updateDate(updateDate)
                    .areaId(MapUtil.getInt(data,"areaId"))
                    .build();
            return player;
        }).filter(Objects::nonNull).collect(Collectors.toList());
    }


    /**
     * 解析小喇叭排行榜
     * @param document
     * @param data
     * @return
     */
    public static List<Trumpet> parseTrumpetRank(Document document, Map<String, Object> data) {
        // 1. 获取所有tr标签的内容
        Elements elements = Xsoup.compile("/html/body/div[1]/table/tbody/tr").evaluate(document).getElements();
        // 解析更新时间
        LocalDate updateDate = parseDate(document);
        // 解析排行榜
        return elements.stream().map(element -> {
            List<String> list = Xsoup.compile("//td/text()").evaluate(element).list();
            if (CollUtil.isEmpty(list)) {
                return null;
            }
            Trumpet trumpet = Trumpet.builder().playName(list.get(1)).trumpetNum(Integer.valueOf(list.get(2)))
                    .updateDate(updateDate).areaId(MapUtil.getInt(data, "areaId"))
                    .build();
            return trumpet;
        }).filter(Objects::nonNull).collect(Collectors.toList());
    }

    public static LocalDate parseDate(Document document){
        // 解析更新时间
        String timeText = Xsoup.compile("/html/body//div[@class='time']/text()").evaluate(document).list().get(0);
        timeText = ReUtil.getGroup0(timeRegex, timeText);
        return LocalDate.parse(timeText, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }
}
